<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<input type="text" placeholder="请输入1-100之间的整数">
<input type="button" value="猜一猜" onclick="f()">
<div></div>
<script>
  //生成一个1-100之间的随机整数
  let x = parseInt(Math.random()*100)+1;
  let i = document.querySelector("input");
  let d = document.querySelector("div");
  let count=0;
  function f(){
    count++;
    if(isNaN(i.value)){
      d.innerText="输入错误";
      return;
    }
    if(x>i.value){
        d.innerText="猜小了";
    }else if(x<i.value){
        d.innerText="猜大了";
    }else{
        d.innerText="恭喜你第"+count+"次猜对啦!";
        x = parseInt(Math.random()*100)+1;
        count=0;
    }
    i.value="";//清空后重新开始猜数
    console.log(x);


  }
</script>
</body>
</html>